package com.zjuee.web.company.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zjuee.base.bean.response.CommonResultCode;
import com.zjuee.base.dto.common.company.CompanyDto;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.enums.DictionaryType;
import com.zjuee.base.enums.RedisCacheType;
import com.zjuee.base.exception.BaseException;
import com.zjuee.base.utils.DateUtils;
import com.zjuee.core.translation.service.TranslateData;
import com.zjuee.core.translation.utils.CaseUtil;
import com.zjuee.core.utils.RedisUtil;
import com.zjuee.service.PlanService;
import com.zjuee.service.auth.service.OrgService;
import com.zjuee.service.company.service.CompanyRegimeService;
import com.zjuee.service.company.service.CompanyService;
import com.zjuee.service.company.service.CompanyWorkDepositoryService;
import com.zjuee.service.company.service.CompanyWorkService;
import com.zjuee.service.company.vo.*;
import com.zjuee.vo.PlanLoginVo;
import com.zjuee.base.model.common.company.Company;
import com.zjuee.base.model.common.company.CompanyWork;
import com.zjuee.base.model.common.company.CompanyWorkDepository;
import com.zjuee.web.company.mapper.CompanyMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private CompanyWorkService workService;

    @Resource
    private CompanyWorkDepositoryService workDepositoryService;

    @Resource
    private OrgService orgService;

    @Resource
    private CompanyRegimeService companyRegimeService;

    @Resource
    private LoginDto loginDto;

    @Resource
    private TranslateData translateData;

    @Resource
    private PlanService planService;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public Long add(CompanyVo companyInsert) {
        return 0L;
    }


    @Override
    public Boolean update(CompanyVo type) {
        return true;
    }

    @Override
    public CompanyVo selectById(Long id, Long validateId) {
        //判断企业Id是否正确
        if(!StringUtils.equalsIgnoreCase(loginDto.getOrgBusinessId().toString(),
                id.toString())){
            throw new BaseException(CommonResultCode.CONSUMER_ERR, "请确认企业Id是否错误");
        }
        //获取基本信息
        Company companyInfo = this.getById(id);
        if (companyInfo == null) {
            throw new BaseException(CommonResultCode.CONSUMER_ERR, "企业信息不存在 - 请确认企业Id是否错误");
        }
        //获取设施信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bci_id", id);
        List<CompanyWork> companyWorkList = workService.list(queryWrapper);
        if (ObjectUtils.isEmpty(companyWorkList)) {
            throw new BaseException(CommonResultCode.CONSUMER_ERR, "设施地址不存在 - 请确认企业信息是否完整");
        }
        //获取仓库信息
        List<CompanyWorkDepository> companyWorkDepositoryList =
                workDepositoryService.list(queryWrapper);
        if (ObjectUtils.isEmpty(companyWorkDepositoryList)) {
            throw new BaseException(CommonResultCode.CONSUMER_ERR, "仓库不存在 - 请确认企业信息是否完整");
        }
        //获取企业制度信息
//        CompanyRegime companyRegime = companyRegimeService.getOne(queryWrapper);

        CompanyVo company = new CompanyVo();

        //完善信息
        CompanyInfoVo companyInfoVo = (CompanyInfoVo) translateData
                .translateData(companyInfo, "corpInfoHandle",
                        String.valueOf(DictionaryType.BCI_TYPE.getValue()),
                        String.valueOf(DictionaryType.BCI_TAG.getValue()),
                        String.valueOf(DictionaryType.BCI_SCALE.getValue()),
                        String.valueOf(RedisCacheType.INDUSTRY_CATEGORY),
                        String.valueOf(RedisCacheType.INDUSTRY_BGROUP),
                        String.valueOf(RedisCacheType.INDUSTRY_MGROUP),
                        String.valueOf(RedisCacheType.INDUSTRY_SGROUP),
                        String.valueOf(RedisCacheType.PROVINCE), String.valueOf(RedisCacheType.CITY),
                        String.valueOf(RedisCacheType.AREA), String.valueOf(RedisCacheType.STREET),
                        String.valueOf(DictionaryType.PB_IS_UPLOAD.getValue()),
                        String.valueOf(DictionaryType.PB_STATUS.getValue()),
                        String.valueOf(DictionaryType.BCI_AUDIT_STATUS.getValue()));
        company.setCompany(companyInfoVo);
        company.setCompanyWorkList(companyWorkList);
        List<CompanyWorkDepositoryVo> companyWorkDepositoryVoList = new ArrayList<>();
        companyWorkDepositoryVoList = CaseUtil
                .castList(translateData.translateData(companyWorkDepositoryList,
                        "corpWorkDepositoryHandle", String.valueOf(RedisCacheType.BCWD_NAME),
                        String.valueOf(RedisCacheType.BCWD_SNAME))
                        , CompanyWorkDepositoryVo.class);
        company.setCompanyWorkDepositoryList(companyWorkDepositoryVoList);

        return company;
    }

    @Override
    public Company selectBaseById(Long id, Long lo) {
        return companyMapper.selectById(id);
    }

    @Override
    public CompanyRspVo selectPartInfoById(Long id) {
        return companyMapper.selectPartInfoById(id);
    }

    @Override
    public Page<Company> list(CompanyDto dto) {
        //根据企业类型、企业名称、审核通过等条件查询
        return companyMapper.selectPageInfo(dto.getPage().convert(), dto);
    }

    @Override
    public LoginRespVo selectLoginInfo() {
        LoginRespVo loginRespVo = new LoginRespVo();
        loginRespVo.setCompanyAuditState(0);
        loginRespVo.setPlanAuditState(0);
        Long id = loginDto.getOrgBusinessId();
        CompanyRspVo companyRspVo = companyMapper.selectPartInfoById(id);
        loginRespVo.setOrgBusinessId(loginDto.getOrgBusinessId());
        loginRespVo.setOrgName(loginDto.getOrgName());
        loginRespVo.setUserName(loginDto.getUserName());
        loginRespVo.setCompanyAuditState(companyRspVo.getBciAuditStatus());
        loginRespVo.setBciType(companyRspVo.getBciType());
        loginRespVo.setBciTag(companyRspVo.getBciTag());
        String companyType = "1";
        //添加企业类型
        if(companyRspVo.getBciType().contains("1") || companyRspVo.getBciType().contains("4")
                || companyRspVo.getBciType().contains("6")){
            companyType = "1";
        }
        if(companyRspVo.getBciType().contains("2") || companyRspVo.getBciType().contains("5")){
            companyType = "3";
        }
        if(companyRspVo.getBciType().contains("3") || companyRspVo.getBciType().contains("7")){
            if(StringUtils.equalsIgnoreCase("3", companyType)){
                companyType = "4";
            }else {
                companyType = "2";
            }
        }
        loginRespVo.setCompanyType(companyType);
        PlanLoginVo plData = planService.selectBriefInfo(id, DateUtils.getYMD(new Date(), "yyyy"));
        if(null == plData || null == plData.getPlanId()){
            loginRespVo.setPlanAuditState(1);
        }else {
            Integer planStatus = plData.getWpiAuditStatus();
            Long planId = plData.getPlanId();
            loginRespVo.setPlanAuditState(planStatus);
            loginRespVo.setPlanId(planId);
        }
        return loginRespVo;
    }

}
